package org.vectrics.recruiter.struts.tag;

import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.ecs.html.A;
import org.apache.log4j.Category;
import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.html.TextHelper;
import org.vectrics.common.pagelist.PageList;
import org.vectrics.common.util.StringUtils;
import org.vectrics.company.Company;
import org.vectrics.company.CompanySelectCriteria;
import org.vectrics.company.web.struts.CompanyPageList;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.web.struts.CompanyListTag;

public class RecruitCompanyListTag extends CompanyListTag  {

	private static final long serialVersionUID = -8074150750166354086L;
	
	private static Category log = Category.getInstance(RecruitCompanyListTag.class);
	public static String NO_ELEMENTS_MESSAGE = "No Companies Found";
	private String parentCompanyId = null;

	public RecruitCompanyListTag() {
		super();
	}

	protected String getNoElementsMessage() {
		return(NO_ELEMENTS_MESSAGE);
	}

	protected PageList createPageList() {
		PageList pageList = null;
		CompanySelectCriteria criteria = new CompanySelectCriteria();
		
		Set domains = CoreServiceLocator.getPersonService()
				.findDomainsWithUserPrivilege(this.getAuthPerson(), Company.PRIVILEGE_COMPANY_VIEW);
		criteria.setDomains(domains);
		if (this.getParentCompanyId() != null) {
		    criteria.setParentId(new Long(this.getParentCompanyId()));
		}
		if (this.getShowOnlyParents().equals("true")) {
			criteria.setNoChildren(true);
		} else {
			criteria.setNoChildren(false);
		}
		pageList = new CompanyPageList(criteria);
		return(pageList);
	}


    public String getDescription() {
    	if ((parentCompanyId != null) && (parentCompanyId.trim().length() <= 0)) {
    		parentCompanyId = null;
    	}

        log.debug("getDescription() called.");
        String description = null;
        try {
	        if (parentCompanyId != null) {
	        	if (log.isDebugEnabled()) {
	        		log.debug("Company provider class = " + CoreServiceLocator.getCompanyService().getClass().getName());
	        	}
	        	Company parentCompany = CoreServiceLocator.getCompanyService().findCompany(new Long(parentCompanyId));
	        	if (parentCompany == null) {
	        		log.error("Parent company not found: " + parentCompanyId);
	        	}
	        	else {
	        		description = "Divisions of " + parentCompany.getName();
	        	}
	        }
        }
        catch (Exception ex) {
        	log.error("Exception finding parent company name - id = " + parentCompanyId, ex);
        }

        return(description);
    }

    public String getPageListLink() {
        return(getContextPath() + "/admin/company/index.do");
    }

    protected void makeHeaderCells(Row row) {
        log.debug("makeHeaderCells() called.");
        row.setDefaultBgImage(this.getContextPath() + "/site-graphics/tbl_hdr.gif");

        Cell cell = row.addCell();
		//cell.setClass("tableHdrBg");
        cell.addElement(TextHelper.getFont("tableHdrFont", "Company Code"));

        cell = row.addCell();
		//cell.setClass("tableHdrBg");
        cell.addElement(TextHelper.getFont("tableHdrFont", "Company Name"));

        cell = row.addCell();
        //cell.setClass("tableHdrBg");
        cell.addElement(TextHelper.getFont("tableHdrFont", "City"));

        cell = row.addCell();
		//cell.setClass("tableHdrBg");
        cell.addElement(TextHelper.getFont("tableHdrFont", "Options"));
    }

    protected void makeObjectRow(HttpServletRequest request, Row row, Company company) {
    	if ((parentCompanyId != null) && (parentCompanyId.trim().length() <= 0)) {
    		parentCompanyId = null;
    	}

        Cell cell = row.addCell();
        cell.addElement(TextHelper.getFont("subFont", company.getReferenceId()));

        cell = row.addCell();
    	cell.addElement(TextHelper.getFont("subFont", company.getName()));
    	try {
    		Collection children = CoreServiceLocator.getCompanyService().findChildCompanies(company.getId(), 0, 1000);

    		if (children.size() > 0) {
    			log.debug("Company has children - displaying links to children");
    			Table table = new Table();
    			table.setCellPadding(3);
    			table.setCellSpacing(3);
    			Iterator iter = children.iterator();
        		while (iter.hasNext()) {
        			Company child = (Company)iter.next();
        			if ((child.getVoided()) || (this.getShowInactive().equals("true"))) {
	        			Row divisionRow = table.addRow();
	        			Cell linkCell = divisionRow.addCell();
	        			A viewChildLink = new A();
	        			viewChildLink.addElement(child.getName());
	        			viewChildLink.setClass("subLink");
	        			viewChildLink.setHref(request.getContextPath() + "/admin/company/view.do?"
	        					+ "companyId=" + child.getId());
	        			linkCell.addElement(viewChildLink);
        			}
        		}
        		cell.addElement("<br>");
        		cell.addElement(table);
    		}
    		else {
    			log.debug("Company has no children");
    		}

    	} catch (Exception ex) {
    		log.error("Exception showing division links", ex);
    	}

        cell = row.addCell();
        String city = null;
        
        if (company.getAddress() != null) {
        	city = company.getAddress().getCity();
        } 
        if (StringUtils.isNullOrBlank(city)) {
        	city = "&nbsp;";
        }
        cell.addElement(TextHelper.getFont("subFont", city));

        cell = row.addCell();
        A link = new A();
        link.setHref(request.getContextPath() + "/admin/company/view.do?"
                + "companyId=" + company.getId());
        link.setClass("subLink");
        link.addElement(this.getPhrase("phrase.view"));
        cell.addElement(link);

/*
		cell.addElement("&nbsp;");
		link = new A();
		link.setHref(request.getContextPath() + "/admin/jobs/index.do?"
				+ "showCompanyJobs=" + company.getId());
		link.setClass("subLink");
		link.addElement("view jobs");
		cell.addElement(link);

		cell.addElement("&nbsp;");
		link = new A();
		link.setHref(request.getContextPath() + "/admin/company/remove.do?"
				+ SaveCompanyAction.ID_PARAM + "=" + company.getId());
		link.setClass("subLink");
		link.addElement("remove");
		cell.addElement(link);


		cell.addElement("&nbsp;");
		link = new A();
		link.setHref(request.getContextPath() + "/admin/company/edit.do?"
				+ "parentId=" + company.getId());
		link.setClass("subLink");
		link.addElement("add division");
		cell.addElement(link);
	*/

    }


    protected void setTableProperties(BorderTable table) {
        table.setWidth(500);
    }


	/**
	 * @return Returns the parentCompanyId.
	 */
	public String getParentCompanyId() {
		return parentCompanyId;
	}

	/**
	 * @param parentCompanyId The parentCompanyId to set.
	 */
	public void setParentCompanyId(String parentCompanyId) {
		this.parentCompanyId = parentCompanyId;
	}

}